/**
 * 用户组权限列表选择
 */

var power;

//页面载入初始化
$( "body" ).ready( function( e )
{
	power = new PowerSelector();
	power.cur = power;
	power.init();
} );

//窗口大小改变
$( window ).resize( function( e )
{
	if( power.visibled() )
	{
		power.relocate();
	}
} );

function power_open( pl, group )
{
	power.open( pl, group );
}

function PowerSelector()
{
	this.cur = null;
	this.html = "<div id='__DIV_POWER_MASK' style='display:block;position:absolute;z-index:8999;background-color:#000;filter:alpha(opacity=20);opacity:0.2;'></div><div id='__DIV_POWER' style='position:absolute;z-index:9000;'><dt><span id='group_name'></span><span>用户组权限选择</span><a id='btn_cancel'>[ 取消 ]</a> <a id='btn_enter'>[ 确定 ]</a></dt><ul></ul></div>";

	// 变量
	var $box;
	var $mask;
	var $list;

	// 初始化
	this.init = function()
	{
		$( "body" ).append( this.html );
		$box = $( "#__DIV_POWER" );
		$mask = $( "#__DIV_POWER_MASK" );
		$list = $box.find( ">ul:first" );

		$box.find( "#btn_cancel" ).click( { cur: this.cur }, function( e )
		{
			var pcur = e.data.cur;
			pcur.close();
		} );
		$box.find( "#btn_enter" ).click( { cur: this.cur }, function( e )
		{
			var pcur = e.data.cur;
			var a = new Array();
			$list.find( "li[class=selected]" ).each( function()
			{
				a.push( $( this ).attr( "rel" ) );
			} );
			pcur.selected_enter( a.join( ";" ) );
			pcur.close();
		} );

		$box.hide();
		$mask.hide();
	};

	// 是否显示
	this.visibled = function()
	{
		return $box.visible();
	};

	// 显示单条信息
	this.open = function( pl, group )
	{
		// 用户组名称
		if( group != undefined )
		{
			$box.find( "#group_name" ).text( "< " + group + " >" );
		}

		// 加载权限列表
		$.post( "power_list.php", function( data )
		{
			$list.append( data );

			$list.find( "li[rel]" ).css( "cursor", "pointer" );
			$list.find( "li[rel]" ).unbind( "click" );
			$list.find( "li[rel]" ).bind( "click", function( e )
			{
				var selected = $( this ).attr( "class" ) == "selected";

				if( selected )
				{
					$( this ).removeClass( "selected" );
				}
				else
				{
					$( this ).addClass( "selected" );
				}
			} );

			var a = pl.split( ";" );

			$list.find( "li[rel]" ).each( function( i )
			{
				var id = $( this ).attr( "rel" );
				if( a.indexOf( id ) > -1 )
				{
					$( this ).addClass( "selected" );
				}
			} );
		} );
		this.relocate();
		$box.slideDown( 150 );
		$mask.fadeIn( 150 );
	};

	this.relocate = function()
	{
		var bw = $( "body" ).width();
		var bh = $( "body" ).height();
		var w = $box.width();
		var h = $box.height();

		$mask.width( bw );
		$mask.height( bh );
		$mask.move( 0, 0 );
		$box.move( ( bw - w ) / 2, ( bh - h ) / 2 * 0.8 );
	};

	this.close = function()
	{
		$list.empty();
		$box.slideUp( 150 );
		$mask.fadeOut( 150 );
	};

	var _power_selected_fn = null;

	this.setfn = function( fn )
	{
		_power_selected_fn = fn;
	};

	this.selected_enter = function( value )
	{
		if( _power_selected_fn != null )
		{
			_power_selected_fn( value );
		}
	};
}
